package com.scene.q7_Thread;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/thread")
public class ThreadController {

    @GetMapping("/testThread1")
    public String testThread1() {
        // 打印当前处理请求的线程名
        String threadName = Thread.currentThread().getName();
        System.out.println("当前处理请求的线程名：" + threadName);
        return "当前线程名：" + threadName;

    }



    // 共享锁对象（关键：两个线程必须用同一个）
    private static final Object lock = new Object();

    @GetMapping("/testThread2")
    public void testThread2() {
        // 线程1：调用wait()，进入等待
        Thread thread1 = new Thread(() -> {
            synchronized (lock) { // 获取lock锁
                try {
                    System.out.println("线程1：获取锁，调用wait()进入等待");
                    lock.wait(); // 释放lock锁，进入等待队列
                    System.out.println("线程1：被唤醒，重新获取锁并继续执行");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "Thread-Wait");


        // 线程2：调用notify()，唤醒等待线程
        Thread thread2 = new Thread(() -> {
            synchronized (lock) { // 获取lock锁（需等thread1释放后才能拿到）
                System.out.println("线程2：获取锁，调用notify()唤醒等待线程");

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                lock.notify(); // 唤醒lock等待队列中的一个线程（此处是thread1）
                System.out.println("线程2：notify()执行完毕，释放锁后退出");
            }
        }, "Thread-Notify");


        thread1.start();
        thread2.start();

    }






    // 不同锁对象：不能唤醒
    private static final Object lock1 = new Object();
    private static final Object lock2 = new Object();

    @GetMapping("/testThread3")
    public void testThread3() {
        // 线程1：用lock1调用wait()
        Thread thread1 = new Thread(() -> {
            synchronized (lock1) {
                try {
                    System.out.println("线程1：用lock1调用wait()，进入等待");
                    lock1.wait(); // 等待lock1的notify()
                    System.out.println("线程1：被唤醒（此句不会执行）");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "Thread-Wait");

        // 线程2：用lock2调用notify()
        Thread thread2 = new Thread(() -> {
            synchronized (lock2) {
                System.out.println("线程2：用lock2调用notify()");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lock2.notify(); // 仅唤醒lock2等待队列的线程
                // lock1.notify(); // 仅唤醒lock1等待队列的线程，抛出异常 current thread is not owner
            }
        }, "Thread-Notify");

        thread1.start();
        thread2.start();

    }



}